Imports System.Globalization
Partial Class Presentation_Unfinished
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        Dim Sql As String
        Dim CountSql As String
        Dim Params(1) As GenParameter
        Dim DateArr(2) As String
        Dim UnfinishedDate As String
        Dim RightNow As Date

        Page.Title = "View Unfinished Activities"

       

        If Not Page.IsPostBack Then

            RightNow = Now.AddHours(11)
            RightNow = RightNow.AddMinutes(30)
            UnfinishedDate = RightNow.Year & "/" & RightNow.Month & "/" & RightNow.Day
        Else
            Page.Validate()
            If Not Page.IsValid Then
                Exit Sub
            End If

            DateArr = txtUnfinishedDate.Text.Split("/")
            UnfinishedDate = DateArr(2) & "/" & DateArr(1) & "/" & DateArr(0)
        End If

        Sql = "SELECT DISTINCT DATE_FORMAT(M.MeetingDate,'%d/%m/%y') AS 'Meeting Date', ClientMaster.ClientName, M.Details, M.NextAction, ContactMaster.ContactName, ContactMaster.Designation, ContactMaster.Mobile, ContactMaster.Telephone, ContactMaster.ContactEmail" & _
            " FROM ((ClientMaster INNER JOIN CompanyAssignments ON ClientMaster.ClientID = CompanyAssignments.ClientID) INNER JOIN ContactMaster ON ClientMaster.ClientID = ContactMaster.ClientID) INNER JOIN WorkflowMaster AS M ON ClientMaster.ClientID = M.ClientID" & _
            " WHERE M.DateofNextAction = ?UnfinishedDate AND ContactMaster.DecisionMaker = 1 AND M.ExecutiveID = ?ExecutiveID AND M.ClientID NOT IN (Select ClientID from WorkflowMaster where InteractionID > M.InteractionID)" & _
            " AND M.NextAction NOT LIKE '%Nothing%'"

        CountSql = "SELECT COUNT(*) FROM (SELECT DISTINCT DATE_FORMAT(M.MeetingDate,'%d/%m/%y') AS 'Meeting Date', ClientMaster.ClientName, M.Details, M.NextAction, ContactMaster.ContactName, ContactMaster.Designation, ContactMaster.Mobile, ContactMaster.Telephone, ContactMaster.ContactEmail" & _
            " FROM ((ClientMaster INNER JOIN CompanyAssignments ON ClientMaster.ClientID = CompanyAssignments.ClientID) INNER JOIN ContactMaster ON ClientMaster.ClientID = ContactMaster.ClientID) INNER JOIN WorkflowMaster AS M ON ClientMaster.ClientID = M.ClientID" & _
            " WHERE M.DateofNextAction = ?UnfinishedDate AND ContactMaster.DecisionMaker = 1 AND M.ExecutiveID = ?ExecutiveID AND M.ClientID NOT IN (Select ClientID from WorkflowMaster where InteractionID > M.InteractionID)" & _
            " AND M.NextAction NOT LIKE '%Nothing%') AS temp"

        Params(0) = New GenParameter("?ExecutiveID", Request.Cookies("Profile").Item("ExecutiveID"))


        If PreviousPage IsNot Nothing Then


            Params(1) = New GenParameter("?UnfinishedDate", PreviousPage.getDate, Data.DbType.Date)
            cstmUnfinished.ShowImmediately = True

        Else
            Params(1) = New GenParameter("?UnfinishedDate", UnfinishedDate, Data.DbType.Date)

        End If

        cstmUnfinished.Sql = Sql
        cstmUnfinished.CountSql = CountSql
        cstmUnfinished.Params = Params
        cstmUnfinished.ShowButton = True



    End Sub

    Protected Sub cstmvdtxtUnfinishedDate_ServerValidate(ByVal source As Object, ByVal args As System.Web.UI.WebControls.ServerValidateEventArgs)
        Dim Day As Integer
        Dim Month As Integer
        Dim Year As Integer
        Dim IsLeap As Boolean
        Dim RightNow As Date
        Dim DateArray As Array
        Dim culture As CultureInfo = New CultureInfo("hi-IN", True)

        rgvdtxtUnfinishedDate.Validate()

        If Not rgvdtxtUnfinishedDate.IsValid Then
            Exit Sub
        End If

        RightNow = Now.AddHours(11)
        RightNow = RightNow.AddMinutes(30)

        DateArray = txtUnfinishedDate.Text.Split("/")

        'Code to check the validity of the date


        Day = DateArray(0)
        Month = DateArray(1)
        Year = DateArray(2)

        'Check Date Validity

        If (Day < 1 Or Day > 31) Then
            cstmvdtxtUnfinishedDate.ErrorMessage = "Day must be between 1 and 31"
            args.IsValid = False
        End If

        If (Month < 1 Or Month > 12) Then
            cstmvdtxtUnfinishedDate.ErrorMessage = "Month must be between 1 and 12"
            args.IsValid = False
        End If

        If ((Month = 4 Or Month = 6 Or Month = 9 Or Month = 11) And Day = 31) Then
            cstmvdtxtUnfinishedDate.ErrorMessage = "Month " & Month & "doesn't have 31 days!"
            args.IsValid = False
        End If

        If (Month = 2) Then

            'Check for February 29th
            IsLeap = (Year Mod 4 = 0 And (Year Mod 100 <> 0 Or Year Mod 400 = 0))

            If (Day > 29 Or (Day = 29 And IsLeap = False)) Then
                cstmvdtxtUnfinishedDate.ErrorMessage = "February " & Year & " doesn't have " & Day & " days!"
                args.IsValid = False

            End If


        End If

    End Sub
End Class
